import http from '../../utils/request'
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
      isShow:false,
      myupload:'',
      tujianlist:[]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let id = options.id
        let title = options.title
        this.setData({myupload:options.myupload?options.myupload:''})
        wx.setNavigationBarTitle({
        　　title:title
        })
        this.getResouceDetail(id)
        this.getResouceTj(id)
        //api判断，基本库版本兼容判断
        if(!wx.getUserProfile){
            wx.showToast({
              title: '更新微信版本',
              icon:'none'
            })
          }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
      // 获取加载的页面对象
      const pages = getCurrentPages();
      // 获取当前页面的对象
      const currentPage = pages[pages.length - 1];
      console.info(currentPage)
      let url= currentPage.route
     let  allurl = url+'?id='+currentPage.options.id+'&title='+currentPage.options.title
      return {
        path:allurl
      }
    },
    async getResouceDetail(id){
        let that = this
        let options={
            url:'/getItemCaseById',
            method:'POST',
            data:{itemCaseId:id},
            header:{"content-type":"application/json;charset=UTF-8"}
        }
        let res = await http(options)
        if(res.data.code==0){
            let extea =res.data.obj.itemName.substring(res.data.obj.itemName.lastIndexOf('.')+1).toLowerCase()
            let dataTime = res.data.obj.createDate.substring(0,10)
            let infolist =  Object.assign(res.data.obj,{suffix:extea,dataTime:dataTime})
            that.setData({detail:infolist})
        }
    },
    //下载
    async downloadFile(e){
      //判断是否登录
      let that = this
      let userInfo = wx.getStorageSync('userInfo') || null
      if(userInfo==null){
        this.setData({isShow:true})
      }else{
        let options={
            url:'/getMyIntegral',
            method:'POST',
            data:{userId:JSON.parse(userInfo).id,authorization:wx.getStorageSync('userInfo')?JSON.parse(wx.getStorageSync('userInfo')).authorization:null},
            header:{"content-type":"application/json;charset=UTF-8"}
        }
        let res = await http(options)
        console.info(res)
        if(res.data.code==0){
          if(res.data.obj>=e.currentTarget.dataset.detail.integral){
            let detail = JSON.stringify(e.currentTarget.dataset.detail)
            wx.setStorageSync('detail', detail)
            wx.navigateTo({
              url: '/pages/orderlist/orderlist?myupload='+that.data.myupload,
            })
          }else{
            wx.showModal({
              title: '提示',
              content: '您的积分不足，请前往充值',
              success (res) {
                if (res.confirm) {
                  wx.navigateTo({
                    url: '/pages/recharge/recharge'
                  })
                }
              }
            })
          }
        }else{
          if(res.data.code==5){
              that.setData({isShow:true})
          }else{
              wx.showToast({
                  title: res.data.msg,
                  icon: 'error',
                  duration: 2000
              })
          }
        }
      }
    },
    // 相关推荐
    async getResouceTj(id){
      let that = this
      let options={
          url:'/getRecommendItemCasesById',
          method:'POST',
          data:{itemcaseId:id,page:1,pagesize:10},
          header:{"content-type":"application/json;charset=UTF-8"}
      }
      let res = await http(options)
      console.info(res)
      if(res.data.code==0){
        let {tujianlist} = that.data
        res.data.obj.resultUitypes.map(item=>{
          let extea = item.itemname.substring(item.itemname.lastIndexOf('.')+1).toLowerCase().replace(/\s+/g,"")
          return Object.assign(item,{suffix:extea})
      })
        let tmpArr = tujianlist.concat(res.data.obj.resultUitypes)
        that.setData({
          tujianlist: tmpArr,
         })
      }
  },
  //资源详情跳转
  linkDetail(e){
    let id = e.currentTarget.dataset.id
    let name = e.currentTarget.dataset.name
    wx.navigateTo({
         url: '/pages/resoucedetail/resoucedetail?id='+id+'&title='+name,
    })
},
})